package sprites;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;

import xml.Xml;

public class CircleSprite extends Sprite {

	public CircleSprite(Point P, int R) {
		super(P);
		setRadius(R);
	}

	private int		radius = 1;

	public boolean containsPoint(Point P) {
		return (P.distance(getPos()) <= radius);
	}

	public int getRadius() {
		return radius;
	}

	public void setRadius(int radius) {
		this.radius = Math.abs(radius);
	}

	public void paintPoint(Graphics G, Rectangle R) {
		Color	oldColor = G.getColor();
		G.setColor(this.getColor());
		G.fillOval(
			R.x - this.radius * R.width,
			R.y - this.radius * R.height,
			R.width*(radius*2+1),
			R.height*(radius*2+1)
		);
		G.setColor(oldColor);
	}

	@Override
	public String toXml() {
		String S = "";
		S = Xml.write(CircleSprite.class.getSimpleName(),
				Xml.write("Location",
					Xml.write( "X", this.getPos().x) +
					Xml.write( "Y", this.getPos().y)
				) +
				Xml.write("Radius", this.getRadius())
			);
		return S;
	}

	@Override
	public void fromXml(String xml) {
		String circleStr	= Xml.getTag(xml, CircleSprite.class.getSimpleName());
		String locationStr	= Xml.getTag(circleStr, "Location");
		String xStr			= Xml.getTag(locationStr, "X");
		String yStr			= Xml.getTag(locationStr, "Y");
		String radiusStr	= Xml.getTag(circleStr, "Radius");

		this.setPos(new Point(Integer.parseInt(xStr), Integer.parseInt(yStr)));
		this.setRadius(Integer.parseInt(radiusStr));
	}

}
